package storage

import (
	"context"
	"fmt"
	"go-caipu/pkg/services/storage/models"
	"io"
)

// Storage 存储接口，定义了所有存储方式需要实现的方法
type Storage interface {
	//Init 初始化存储客户端
	Init(config models.StorageConfig) error
	//文件上传功能
	Upload(ctx context.Context, reader io.Reader, storage *models.FileStorage) (*models.FileStorage, error)
	//Download 获取文件
	Download(ctx context.Context, storagePath, fileKey string) (io.ReadCloser, error)

	//Delete 删除文件
	Delete(ctx context.Context, storagePath, fileKey string) error

	//GetFileURL 获取文件访问URL
	GetFileURL(ctx context.Context, storagePath, fileKey string) (string, error)
}

// Part 分片信息
type Part struct {
	PartNumber int
	ETag       string
}

// 存储类型注册器
var storageRegistry = make(map[models.StorageType]func() Storage)

// Register 注册存储类型
func Register(storageType models.StorageType, factory func() Storage) {
	storageRegistry[storageType] = factory
}

// NewStorage 创建存储实例
func NewStorage(storageType models.StorageType) (Storage, error) {
	factory, ok := storageRegistry[storageType]
	if !ok {
		return nil, fmt.Errorf("unsupported storage type: %s", storageType)
	}
	return factory(), nil
}
